<!--
    Copyright 2010 Aravindan Ramkumar

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License. You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and limitations under the License.
-->
<html>
<head>
    <script type="text/javascript" src="lib/yahoo-dom-event.js"></script>    
    <script type="text/javascript" src="lib/json-min.js"></script>
    <script type="text/javascript" src="lib/connection-min.js"></script>
    <script type="text/javascript">
        var BING_SERVICE_URL = "http://api.search.live.net/json.aspx?Appid=719CF822A04CC49B98942128E67F6FFADA62645C&sources=web&web.count=10&query=site:{site} {query}&web.offset={start}";        
        var YAHOO_SERVICE_URL = "http://boss.yahooapis.com/ysearch/web/v1/{query}?appid=tXtSG3_V34FrTbs0sAEIfkWDNI7bjH2oW6YvMlG4uNDJpjOH5ykV22zLsG4syfY-&format=json&style=raw&sites={site}&start={start}";
        /**
         * Send a request to the content script, to get the selected text. Use the callback to pass it back to browser action.
         */
        function getSelection(tabId, callback) {
            chrome.tabs.sendRequest(tabId, {}, function(response) {
                if (response.selectedText) {
                    callback(response.selectedText);
                }
            });
        }

        var bingCallback = {
            success : function(o) {
                var data = YAHOO.lang.JSON.parse(o.responseText);
                var response = {};

                response['total'] = data.SearchResponse.Web.Total;
                response['start'] = data.SearchResponse.Web.Offset;
                response['results'] = [];

                if (data.SearchResponse.Web.Results) {
                    for (var index = 0; index < data.SearchResponse.Web.Results.length; index++) {
                        var result = {};
                        result['url'] = data.SearchResponse.Web.Results[index].Url;
                        result['title'] = data.SearchResponse.Web.Results[index].Title;
                        result['description'] = data.SearchResponse.Web.Results[index].Description;
                        result['displayUrl'] = data.SearchResponse.Web.Results[index].DisplayUrl;
                        response.results.push(result);
                    }
                }

                // call the popup page
                this.successCallback(response);
            },

            failure : function(o) {
                console.log("Status: " + o.status + " Message: " + o.statusText);
                var response = {'error' : "There was error communicating to Bing search service"};

                // call the popup page
                this.failureCallback(response);
            }
        };

        var yahooCallback = {
            success : function(o) {
                var data = YAHOO.lang.JSON.parse(o.responseText);
                var response = {};

                response['total'] = parseInt(data.ysearchresponse.totalhits, 10);
                response['start'] = parseInt(data.ysearchresponse.start, 10);
                response['results'] = [];

                if (data.ysearchresponse.resultset_web) {
                    for (var index = 0; index < data.ysearchresponse.resultset_web.length; index++) {
                        var result = {};
                        result['url'] = data.ysearchresponse.resultset_web[index].url;
                        result['title'] = data.ysearchresponse.resultset_web[index].title;
                        result['description'] = data.ysearchresponse.resultset_web[index].abstract;
                        result['displayUrl'] = data.ysearchresponse.resultset_web[index].dispurl;
                        response.results.push(result);
                    }
                }

                // call the popup page
                this.successCallback(response);
            },
            
            failure : function(o) {
                console.log("Status: " + o.status + " Message: " + o.statusText);
                var response = {'error' : "There was error communicating to Yahoo search service"};

                // call the popup page
                this.failureCallback(response);
            }
        };

        function search(request) {
            var url, xhrCallback;
            var provider = localStorage['provider'] || 'bing';
            // select url and callback
            if(provider == 'bing') {
                url = BING_SERVICE_URL;
                xhrCallback = bingCallback;
            } else if(provider == 'yahoo') {
                url = YAHOO_SERVICE_URL;                
                xhrCallback = yahooCallback;
            }

            // replace parameters
            url = url.replace('{site}',request.domainName).replace('{query}',request.textToSearch).replace("{start}",request.start);

            // setup callbacks
            xhrCallback['successCallback'] = request.success;
            xhrCallback['failureCallback'] = request.failure;
            YAHOO.util.Connect.asyncRequest('GET', url, xhrCallback);
        }

    </script>
</head>
<body></body>
</html> 
